﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Commands;
using DXTools.UI.Core.Events;
using DXTools.UI.Core.View;

namespace DXTools.UI.Core.ViewModel
{
    public abstract class ViewModelBase : IViewModelBase
    {
        #region Properties

        public IRegionManager RegionManager { get; set; }
        public IEventAggregator EventAggregator { get; set; }
        public IUnityContainer UnityContainer { get; set; }

        public ViewBase View { get; set; }

        private bool _isBusy;
        public bool IsBusy 
        {
            get
            {
                return _isBusy;
            }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        private string _headerInfo;
        public string HeaderInfo
        {
            get
            {
                return _headerInfo;
            }
            set
            {
                _headerInfo = value;
                RaisePropertyChanged("HeaderInfo");
            }
        }

        #endregion

        #region Constructors

        public ViewModelBase()
        {
 
        }

        public ViewModelBase(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer container)
        {
            this.EventAggregator = eventAggregator;
            this.RegionManager = regionManager;
            this.UnityContainer = container;
        }

        #endregion

        #region Notification changed

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Close View

        public DelegateCommand<object> CloseViewCommand { get; protected set; }

        public void CloseView()
        {
            var bodyRegion = RegionManager.Regions[RegionNames.BODY_REGION];
            bodyRegion.Remove(this.View);
            EventAggregator.GetEvent<ViewClosedEvent>().Publish(this.View);

            //var bodyRegion = RegionManager.Regions[RegionNames.BODY_REGION];
            
            //UserControl closedView = null;

            //foreach (var view in bodyRegion.ActiveViews)
            //{
            //    var viewType = view.GetType();
            //    if (viewType.FullName != viewName)
            //        continue;

            //    bodyRegion.Remove(view);
            //    closedView = view as UserControl;
            //    break;
            //}

            //EventAggregator.GetEvent<ViewClosedEvent>().Publish(closedView);
        }

        #endregion

    }
}
